home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / barg / barg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-07-12  |  9.4 KB  |  425 lines

  1. /* 
  2.  * barg.c --
  3.  *
  4.  *    Bar graph generating program.  See the man page for details on
  5.  *    how to use this program.
  6.  *
  7.  * Copyright (C) 1986 Regents of the University of California
  8.  * All rights reserved.
  9.  */
  10.  
  11. #include <stdio.h>
  12.  
  13. #define    BUF_LENGTH    512
  14. char    outFile[100];
  15. int     xMinPixel = 128;
  16. int     yMinPixel = 0;
  17. int     xMaxPixel = 960;
  18. int     yMaxPixel = 384;
  19. int     numSets = -1;
  20. int     numPerSet = -1;
  21. int     xWidth = -1;
  22. float     yMaxVal;
  23. float    yValInc;
  24. float    yBaseVal;
  25. int    yBaseline;
  26.  
  27. int     yNum = -1;
  28. int     leftBorder = 48;
  29. int     rightBorder = 24;
  30. int     innerBarSpace = 8;
  31. int     innerSetSpace = -1;
  32. int     curXVal;
  33. int     pixPerSet;
  34. int     printPercent = 0;
  35. char    xLabel[100];
  36. char    yLabel[100];
  37. char    graphTitle[100];
  38. int    xLabeled = 0;
  39. int    yLabeled = 0;
  40. int    graphTitled = 0;
  41.  
  42. #define    MAX_INNER_SET_SPACE    64
  43. #define    MAX_X_WIDTH        32
  44. #define MAX_BARS        8
  45. #define INNER_SET_SPACE        32
  46. #define X_WIDTH            16
  47. #define FONT_HEIGHT        15
  48.  
  49. char    barLabels[MAX_BARS][100];
  50. int    barTypes[MAX_BARS];
  51. int    gremFilledPolys[MAX_BARS] = {1, 3, 12, 14, 16, 19, 21, 23};
  52. FILE    *inFP, *outFP;
  53. char    buf[BUF_LENGTH];
  54.  
  55. int    leftKeyBorder = 64;
  56. int    rightKeyBorder = 32;
  57. int    keyWidth = 48;
  58. int    keyHeight = 24;
  59. int    yBaseline;
  60.  
  61. main(argc, argv)
  62.     int     argc;
  63.     char **argv;
  64. {
  65.     int        i;
  66.  
  67.     if (argc <= 1) {
  68.         inFP = stdin;
  69.     outFP = stdout;
  70.     } else {
  71.     inFP = fopen(argv[1], "r");
  72.     if (inFP == NULL) {
  73.         fprintf(stderr, "Couldn't open %s for reading\n", argv[1]);
  74.         exit(1);
  75.     }
  76.     if (argc <= 2) {
  77.         outFP = stdout;
  78.     } else {
  79.         outFP = fopen(argv[2], "w");
  80.         if (outFP == NULL) {
  81.         fprintf(stderr, "Couldn't open %s for writing\n", argv[2]);
  82.         exit(1);
  83.         }
  84.     }
  85.     }
  86.     fprintf(outFP, "sungremlinfile\n");
  87.     fprintf(outFP, "1 %d %d\n", xMinPixel, yMinPixel);
  88.     while (fgets(buf, BUF_LENGTH, inFP) != NULL) {
  89.     switch (buf[0]) {
  90.         case 'b':
  91.         sscanf(&buf[1], "%d %d", &leftBorder, &rightBorder);
  92.         break;
  93.         case 'd':
  94.         DrawBars();
  95.         break;
  96.         case 'h':
  97.             sscanf(&buf[1], "%d", &yMaxPixel);
  98.         break;
  99.         case 'k':
  100.         sscanf(&buf[1], "%d %d %d %d", &leftKeyBorder, &rightKeyBorder,
  101.                         &keyWidth, &keyHeight);
  102.         break;
  103.         case 'l': {
  104.         if (buf[1] == 'x') {
  105.             GetString(&buf[2], xLabel);
  106.             xLabeled = 1;
  107.         } else if (buf[1] == 'y') {
  108.             GetString(&buf[2], yLabel);
  109.             yLabeled = 1;
  110.         } else {
  111.             fprintf(stderr, "Unknown label type\n");
  112.         }
  113.         break;
  114.         }
  115.         case 'n': {
  116.         GetSetInfo();
  117.         break;
  118.         }
  119.         case 'p':
  120.         printPercent = 1;
  121.         break;
  122.         case 's':
  123.         sscanf(&buf[1], "%d %d", &innerBarSpace, &innerSetSpace);
  124.         break;
  125.         case 't':
  126.         GetString(&buf[1], graphTitle);
  127.         graphTitled = 1;
  128.         break;
  129.         case 'w':
  130.         sscanf(&buf[1], "%d", &xMaxPixel);
  131.         break;
  132.         case 'y': {
  133.         sscanf(&buf[1], "%d %f %f", &yNum, &yValInc, &yBaseVal);
  134.         yMaxVal = yNum * yValInc + yBaseVal;
  135.         if (yBaseVal < 0) {
  136.             yBaseline = (-yBaseVal / (yMaxVal - yBaseVal)) *
  137.                     (yMaxPixel - yMinPixel) + yMinPixel;
  138.         } else {
  139.             yBaseline = yMinPixel;
  140.         }
  141.         break;
  142.         }
  143.         case 'W': 
  144.         sscanf(&buf[1], "%d", &xWidth);
  145.         break;
  146.         case '1':
  147.         case '2':
  148.         case '3':
  149.         case '4':
  150.         case '5':
  151.         case '6':
  152.         case '7':
  153.         case '8': {
  154.         int    barNum;
  155.         char    *strPtr;
  156.  
  157.         barNum = buf[0] - '0' - 1;
  158.         sscanf(&buf[1], "%d", &barTypes[barNum]);
  159.         strPtr = &buf[1];
  160.         while (*strPtr == ' ') {
  161.             strPtr++;
  162.         }
  163.         while (*strPtr != ' ') {
  164.             strPtr++;
  165.         }
  166.         GetString(strPtr, barLabels[barNum]);
  167.         break;
  168.         }
  169.         case '#':
  170.         case '\n':
  171.         case ' ':
  172.         break;
  173.         default:
  174.         fprintf(stderr, "Unknown command %s\n", buf);
  175.         break;
  176.     }
  177.     }
  178.     /*
  179.      * Print the X and Y axises.
  180.      */
  181.     fprintf(outFP, "VECTOR\n");
  182.     fprintf(outFP, "%d %d\n%d %d\n", xMinPixel, yBaseline, xMaxPixel,
  183.                      yBaseline);
  184.     fprintf(outFP, "*\n6 0\n0\n");
  185.     fprintf(outFP, "VECTOR\n");
  186.     fprintf(outFP, "%d %d\n%d %d\n", xMinPixel, yMinPixel, xMinPixel, 
  187.                      yMaxPixel);
  188.     fprintf(outFP, "*\n6 0\n0\n");
  189.  
  190.     /*
  191.      * Print the dashed cross bars and labels here.
  192.      */
  193.     for (i = 0; i <= yNum; i++) {
  194.     int    yPixel;
  195.     char    label[100];
  196.  
  197.     yPixel = yMinPixel + (float)(yMaxPixel - yMinPixel) / yNum * i;
  198.     if (yPixel != yBaseline) {
  199.         fprintf(outFP, "VECTOR\n");
  200.         fprintf(outFP, "%d %d\n%d %d\n", xMinPixel, yPixel, 
  201.                          xMaxPixel, yPixel);
  202.         fprintf(outFP, "*\n1 0\n0\n");
  203.     }
  204.     if (yValInc - (int)yValInc == 0.0) {
  205.         sprintf(label, "%d", (int) (i * yValInc + yBaseVal));
  206.     } else {
  207.         sprintf(label, "%0.2f", i * yValInc + yBaseVal);
  208.     }
  209.     if (printPercent) {
  210.         strcat(label, "%");
  211.     }
  212.     PutString(label, xMinPixel - 10, yPixel, "CENTRIGHT", 2);
  213.     }
  214.  
  215.     /*
  216.      * Print the axis labels.
  217.      */
  218.     if (xLabeled) {
  219.     PutString(xLabel, xMinPixel + (xMaxPixel - xMinPixel) / 2,
  220.           yMinPixel - 48, "TOPCENT", 2);
  221.     }
  222.     if (yLabeled) {
  223.     int    yPix;
  224.     char    chBuf[2];
  225.     char    *strPtr;
  226.  
  227.     chBuf[1] = '\0';
  228.     yPix = yMaxPixel - 
  229.            (yMaxPixel - yMinPixel - strlen(yLabel) * FONT_HEIGHT) / 2;
  230.     for (strPtr = yLabel;
  231.          *strPtr != '\0';
  232.          strPtr++, yPix -= FONT_HEIGHT) {
  233.         chBuf[0] = *strPtr;
  234.         PutString(chBuf, xMinPixel - 80, yPix, "TOPCENT", 2);
  235.     }
  236.     }
  237.  
  238.     /*
  239.      * Print the graph title.
  240.      */
  241.     if (graphTitled) {
  242.     PutString(graphTitle, xMinPixel + (xMaxPixel - xMinPixel) / 2,
  243.            yMaxPixel + 64, "TOPCENT", 3);
  244.     }
  245.  
  246.     /*
  247.      * Print out bar key.
  248.      */
  249.     PrintBarKey();
  250.  
  251.     fprintf(outFP, "-1\n");
  252.     exit(0);
  253. }
  254.  
  255. PutString(label, x, y, loc, font)
  256.     char    *label;
  257.     int        x;
  258.     int        y;
  259.     char    *loc;
  260.     int        font;
  261. {
  262.     fprintf(outFP, "%s\n", loc);
  263.     fprintf(outFP, "%d %d\n", x, y);
  264.     fprintf(outFP, "0 0\n0 0\n0 0\n*\n1 %d\n", font);
  265.     fprintf(outFP, "%d %s\n", strlen(label), label);
  266. }
  267.  
  268. GetString(srcStr, destStr)
  269.     char    *srcStr;
  270.     char    *destStr;
  271. {
  272.     char    *strPtr;
  273.  
  274.     while (*srcStr == ' ') {
  275.     srcStr++;
  276.     }
  277.     strPtr = srcStr;
  278.     while (*strPtr != '\n') {
  279.     strPtr++;
  280.     }
  281.     *strPtr = '\0';
  282.     strcpy(destStr, srcStr);
  283. }
  284.  
  285. DrawFilledRectangle(minX, minY, width, height, fillType)
  286.     int    minX;
  287.     int    minY;
  288.     int width;
  289.     int height;
  290.     int fillType;
  291. {
  292.     fprintf(outFP, "POLYGON\n");
  293.     fprintf(outFP, "%d %d\n", minX, minY + height);
  294.     fprintf(outFP, "%d %d\n", minX + width, minY + height);
  295.     fprintf(outFP, "%d %d\n", minX + width, minY);
  296.     fprintf(outFP, "%d %d\n", minX, minY);
  297.     fprintf(outFP, "*\n");
  298.     fprintf(outFP, "5 %d\n", gremFilledPolys[fillType]);
  299.     fprintf(outFP, "0\n");
  300.  
  301. }
  302.  
  303. PrintBarKey()
  304. {
  305.     int    perKeyWidth;
  306.     int    baseX;
  307.     int    baseY;
  308.     int    i;
  309.  
  310.     perKeyWidth = (xMaxPixel - xMinPixel - leftKeyBorder - rightKeyBorder) / 
  311.                                     numPerSet;
  312.     if (perKeyWidth < keyWidth * 3) {
  313.     perKeyWidth = keyWidth * 3;
  314.     }
  315.     if (xLabeled) {
  316.     baseY = yMinPixel - keyHeight - 96;
  317.     } else {
  318.     baseY = yMinPixel - keyHeight - 64;
  319.     }
  320.     for (i = 0; i < numPerSet; i++) {
  321.     baseX = xMinPixel + leftKeyBorder + perKeyWidth * i;
  322.     DrawFilledRectangle(baseX, baseY, keyWidth, keyHeight, barTypes[i]);
  323.     PutString(barLabels[i], baseX + keyWidth + 16, baseY + keyHeight / 2,
  324.           "CENTLEFT", 2);
  325.     }
  326. }
  327.  
  328. DrawBars()
  329. {
  330.     float    val;
  331.     int        yHeight;
  332.     char    label[100];
  333.     char    *labelPtr;
  334.     int        i = 0;
  335.  
  336.     i = 1;
  337.     while (buf[i] == ' ') {
  338.     i++;
  339.     }
  340.     for (labelPtr = label; buf[i] != '\n'; i++, labelPtr++) {
  341.     *labelPtr = buf[i];
  342.     }
  343.     *labelPtr = '\0';
  344.  
  345.     PutString(label, curXVal + pixPerSet / 2, yMinPixel - 10,
  346.           "TOPCENT", 2);
  347.     i = 0;
  348.     while (fgets(buf, BUF_LENGTH, inFP) != NULL) {
  349.     if (buf[0] == 'e') {
  350.         break;
  351.     }
  352.     if (i >= numPerSet) {
  353.         fprintf(stderr, "Too many bars per set \"%s\"\n", label);
  354.         i++;
  355.         continue;
  356.     }
  357.     sscanf(buf, "%f", &val);
  358.     if (val > yMaxVal) {
  359.         fprintf(stderr, "Y Value %4.2f clipped\n", val);
  360.         val = yMaxVal;
  361.     } else if (val < yBaseVal) {
  362.         fprintf(stderr, "Y Value %4.2f clipped\n", val);
  363.         val = yBaseVal;
  364.     }
  365.     if (yBaseVal >= 0) {
  366.         yHeight = ((val - yBaseVal) / (yMaxVal - yBaseVal)) * 
  367.                             (yMaxPixel - yMinPixel);
  368.     } else if (val >= 0) {
  369.         yHeight = (val / yMaxVal) * (yMaxPixel - yBaseline);
  370.     } else {
  371.         yHeight = (val / yBaseVal) * (yBaseline - yMinPixel);
  372.     }
  373.     if (yHeight < 4) {
  374.         fprintf(outFP, "VECTOR\n");
  375.         fprintf(outFP, "%d %d\n%d %d\n", curXVal, yBaseline + 2,                    curXVal + xWidth, yBaseline + 2);
  376.         fprintf(outFP, "*\n3 0\n0\n");
  377.     } else {
  378.         if (val < 0) {
  379.         DrawFilledRectangle(curXVal, yBaseline - yHeight, xWidth,
  380.                     yHeight, barTypes[i]);
  381.         } else {
  382.         DrawFilledRectangle(curXVal, yBaseline, xWidth,
  383.                     yHeight, barTypes[i]);
  384.         }
  385.     }
  386.     i++;
  387.     curXVal += xWidth + innerBarSpace;
  388.     }
  389.     curXVal += innerSetSpace - innerBarSpace;
  390. }
  391.  
  392. GetSetInfo()
  393. {
  394.     int    availPixels;
  395.     int    width;
  396.     
  397.     sscanf(&buf[1], "%d %d", &numSets, &numPerSet);
  398.     availPixels = xMaxPixel - xMinPixel - leftBorder - rightBorder;
  399.     if (innerSetSpace == -1) {
  400.     if (xWidth == -1) {
  401.         xWidth = X_WIDTH;
  402.     }
  403.     availPixels -= xWidth * numSets * numPerSet +
  404.                innerBarSpace * numSets * (numPerSet - 1);
  405.     innerSetSpace = availPixels / (numSets - 1);
  406.     if (innerSetSpace > MAX_INNER_SET_SPACE) {
  407.         innerSetSpace = MAX_INNER_SET_SPACE;
  408.     }
  409.     } else if (xWidth == -1) {
  410.     availPixels -= (numSets - 1) * innerSetSpace;
  411.     pixPerSet = availPixels / numSets;
  412.     xWidth = (pixPerSet - (numPerSet - 1) * innerBarSpace) / 
  413.                             numPerSet;
  414.     if (xWidth > MAX_X_WIDTH) {
  415.         xWidth = MAX_X_WIDTH;
  416.     }
  417.     }
  418.     pixPerSet = xWidth * numPerSet + 
  419.         innerBarSpace * (numPerSet - 1);
  420.     width = leftBorder + rightBorder + pixPerSet * numSets + 
  421.         innerSetSpace * (numSets - 1);
  422.     xMaxPixel = xMinPixel + width;
  423.     curXVal = xMinPixel + leftBorder;
  424. }
  425.